package com.ruoyi.system.interAttr.attr;

import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.system.interAttr.domain.GamesRes;
import com.ruoyi.system.interAttr.domain.InterAttrRes;
import com.ruoyi.system.interAttr.domain.InterServerRes;
import com.ruoyi.system.model.domain.SalePartner;
import com.ruoyi.system.outto.core.MapUntils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 密码游戏列表没有接口
 *
 * 等对面开发人员写好   2024 06 27
 *
 */
@Slf4j
@Service
public class AMAttr implements AdapterAttr {


    public static void main(String[] args) {
        Map<String, Object> params = new HashMap<>();
        params.put("opide","89929af8-a866-4417-965a-b5831d46c1d4");
        String cover = MapUntils.getCover(params)+"key=39367653-5bd1-4f9a-9ee3-0596d5f0b1f7";
        System.out.println(cover);
        String sign = DigestUtils.md5Hex(cover).toUpperCase();
        params.put("sign",sign);
        String url = "https://zx.amyoux.com/prod-api/PulsApi/getGmDicdate";
        String response = HttpRequest.post(url).body(JSONObject.toJSONString(params)).execute().body();
        System.out.println(response);
    }

    @Override
    public List<GamesRes> getGames(SalePartner salePartner) {
        Map<String, Object> params = new HashMap<>();
        params.put("opide",salePartner.getSaleKey());
        String cover = MapUntils.getCover(params)+"key="+salePartner.getOther();
        System.out.println(cover);
        String sign = DigestUtils.md5Hex(cover).toUpperCase();
        params.put("sign",sign);
        String url = "https://zx.amyoux.com/prod-api/PulsApi/getGmDicdate";
        String result = HttpRequest.post(url).body(JSONObject.toJSONString(params)).execute().body();
        JSONObject resJson = JSONObject.parseObject(result);
        List<JSONObject> jsonObjects = JSONObject.parseArray(resJson.getString("data"), JSONObject.class);
        List<GamesRes> gamesResList = new ArrayList<>();

        for (JSONObject jsonObject : jsonObjects) {
            GamesRes gamesRes = new GamesRes();
            String id = jsonObject.getString("dictCode");
            String title = jsonObject.getString("dictLabel");
            gamesRes.setId(id);
            gamesRes.setGameName(title);
            gamesResList.add(gamesRes);
        }
        return gamesResList;
    }

    @Override
    public List<InterServerRes> getServer(String gameId, SalePartner salePartner) {
        Map<String, Object> params = new HashMap<>();
        params.put("opide",salePartner.getSaleKey());
        params.put("dictCode",gameId);
        String cover = MapUntils.getCover(params)+"key="+salePartner.getOther();
        String sign = DigestUtils.md5Hex(cover).toUpperCase();
        params.put("sign",sign);
        String url = "https://zx.amyoux.com/prod-api/PulsApi/getAttribute";
        String response = HttpRequest.post(url).body(JSONObject.toJSONString(params)).execute().body();
        JSONObject jsonObject = JSONObject.parseObject(response);
        List<JSONObject> data1 = JSONObject.parseArray(jsonObject.getString("data"), JSONObject.class);
        List<JSONObject> data = JSONObject.parseArray(data1.get(0).getString("children"), JSONObject.class);
        List<InterServerRes> interServerRes = new ArrayList<>();
        for (JSONObject datum : data) {
            InterServerRes interServer = new InterServerRes();
            interServer.setId(datum.getString("id"));
            interServer.setName(datum.getString("gameField"));
//            String servers = datum.getString("serverlist");
//            if(StringUtils.isNotEmpty(servers)) {
//                List<InterServerRes> interServerRes1 = new ArrayList<>();
//                List<JSONObject> jsonObjects = JSONObject.parseArray(servers, JSONObject.class);
//                for (JSONObject object : jsonObjects) {
//                    InterServerRes interServer1 = new InterServerRes();
//                    String id1 = object.getString("id");
//                    String title1 = object.getString("name");
//                    interServer1.setId(id1);
//                    interServer1.setName(title1);
//                    interServerRes1.add(interServer1);
//                }
//                interServer.setInterServerRes(interServerRes1);
//            }
            interServerRes.add(interServer);
        }
        return interServerRes;
    }

    @Override
    public List<InterAttrRes> getPropety(String gameId, String type, SalePartner salePartner) {
        Map<String, Object> params = new HashMap<>();
        params.put("opide",salePartner.getSaleKey());
        params.put("dictCode",gameId);
        String cover = MapUntils.getCover(params)+"key="+salePartner.getOther();
        String sign = DigestUtils.md5Hex(cover).toUpperCase();
        params.put("sign",sign);
        String url = "https://zx.amyoux.com/prod-api/PulsApi/getAttribute";
        String response = HttpRequest.post(url).body(JSONObject.toJSONString(params)).execute().body();
        List<InterAttrRes> interAttrResList = new ArrayList<>();
        JSONObject jsonObject = JSONObject.parseObject(response);
        List<JSONObject> data = JSONObject.parseArray(jsonObject.getString("data"), JSONObject.class);
        List<JSONObject> collect = new ArrayList<>();
        if (Objects.equals("1",type)){
            collect = data.stream().filter(d -> d.getString("fieldType").equals("radio")).collect(Collectors.toList());
        }else if  (Objects.equals("2",type)){
            collect = data.stream().filter(d -> d.getString("fieldType").equals("checkbox")).collect(Collectors.toList());
        }else if (Objects.equals("3",type)){
            collect = data.stream().filter(d -> d.getString("fieldType").equals("input")).collect(Collectors.toList());
        }
        for (JSONObject object : collect) {
            String string = object.getString("children");
            JSONArray jsonArray = JSONArray.parseArray(string);
            InterAttrRes interAttrRes = new InterAttrRes();
            if(Objects.equals("2",type)) {
                interAttrRes.setId(object.getString("gameKey"));
            }else {
                interAttrRes.setId(object.getString("id"));
            }
            interAttrRes.setName(object.getString("gameField"));
            List<InterAttrRes> interAttrResList1 = new ArrayList<>();
            for(Object o : jsonArray) {
                InterAttrRes interAttrRes1 = new InterAttrRes();
                interAttrRes1.setId(JSONObject.parseObject(o.toString()).getString("id"));
                interAttrRes1.setName(JSONObject.parseObject(o.toString()).getString("gameField"));
                interAttrResList1.add(interAttrRes1);
                interAttrRes.setInterServerRes(interAttrResList1);
            }
            interAttrResList.add(interAttrRes);
        }
        return interAttrResList;
    }
}